package com.hngy.xpq.adapterdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hngy.xpq.adapterdemo.R;
import com.hngy.xpq.adapterdemo.bean.Data;

import java.util.List;

public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

    private List<Data> dataList;

    //绑定android界面控件
    static class ViewHolder extends RecyclerView.ViewHolder {

        View fileView;
        ImageView imageGrid;
        TextView nameGrid;
        public ViewHolder(View itemView) {
            super(itemView);
            fileView = itemView;
            imageGrid = itemView.findViewById(R.id.imageGrid);
            nameGrid = itemView.findViewById(R.id.nameGrid);
        }
    }

    // 获取到数据
    public GridAdapter(List<Data> list) {
        // 初始化点击数据
        this.dataList = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid,parent,false);
        final ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final Data d = dataList.get(position);
        holder.imageGrid.setImageResource(d.getImage());
        /** --------------------end---------------------- **/
        holder.nameGrid.setText(d.getName());
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

}
